package com.kalvin.kvf.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.dep.entity.Adv;
import com.kalvin.kvf.modules.dep.service.AdvService;
import com.kalvin.kvf.modules.sys.entity.Approve;
import com.kalvin.kvf.modules.sys.service.ApproveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @since 2021-04-21 21:34:12
 */
@RestController
@RequestMapping("sys/approve")
public class ApproveController extends BaseController {

    @Autowired
    private ApproveService approveService;

    @Autowired
    private AdvService advService;

    @RequiresPermissions("sys:approve:info")
    @GetMapping("info")
    public Object info(){
       List<String> res =  approveService.selectContent();
        return JSON.toJSON(res);
    }

    @RequiresPermissions("sys:approve:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("sys/approve");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("sys/approve_edit");
        Approve approve;
        if (id == null) {
            approve = new Approve();
        } else {
            approve = approveService.getById(id);
        }
        mv.addObject("editInfo", approve);
        return mv;
    }

//    @GetMapping(value = "pass")
//    public String pass(){
//        Approve
//    }

    @GetMapping(value = "list/data")
    public R listData(Approve approve) {
        Page<Approve> page = approveService.listApprovePage(approve);
        return R.ok(page);
    }

//    @RequiresPermissions("sys:approve:edit")
    @PostMapping(value = "edit")
    public R edit(Approve approve) {
        Adv adv = new Adv();
        if("0".equals(approve.getStatus())){
            approve.setStatus("通过");
        }else if("1".equals(approve.getStatus())){
            approve.setStatus("退回");
        }else{
            approve.setStatus("待审批");
        }
        adv.setId(approve.getId());
        adv.setStatus(approve.getStatus());
        adv.setContent(approve.getContent());
        adv.setCreatime(approve.getCreatime());
        adv.setName(approve.getName());
        approveService.updateById(approve);
        advService.updateById(adv);

        return R.ok();
    }

    @RequiresPermissions("sys:approve:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        approveService.removeById(id);
        return R.ok();
    }

    @RequiresPermissions("sys:approve:back")
    @PostMapping(value = "back")
    public R back() {
        return R.ok("该功能未实现");
    }

    @RequiresPermissions("sys:approve:pass")
    @PostMapping(value = "pass")
    public R pass() {
        return R.ok("该功能未实现");
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(approveService.getById(id));
    }

}

